/**
 * Copyright (c) suzuki ryoutoku
 * See this code regarding the Apache License Version 2.0
 * @author suzuki ryoutoku
 */

//=============================================================================
//	Pre processor
//=============================================================================
#include "Coordinate.h"
#include <GL/glx.h>
#include <GL/gl.h>
using namespace library::mathematics;

//=============================================================================
//	Define Coordinate's method
//=============================================================================
/**
 * @author suzuki ryoutoku
 * @date 2007-6-14
 * @version 1.0
 */
Coordinate::Coordinate()
{
  this->m_pivot.x = this->m_pivot.y = this->m_pivot.z = 0.0;
  this->m_pivot.w = 1.0;
  this->setM_scale(Vector<double>(1.0,1.0,1.0,0.0));
}

/**
 * @author suzuki ryoutoku
 * @date 2007-6-14
 * @version 1.0
 */
Coordinate::~Coordinate()
{
}

/**
 * Transform vector
 * @param arg target vector
 * @return transformed vector
 * @author suzuki ryoutoku
 * @date 2007-7-18
 * @version 1.0
 */
Vector<double>	Coordinate::transform(const Vector<double>& arg)
{
#if 0
  Vector<double> retval = this->m_matrix * arg;
  return (retval += this->m_pivot);
#endif
  Vector<double> retval = arg - this->m_local_origin;
  retval = this->m_matrix * retval;
  return (retval += this->m_pivot);
}

/**
 * To set coordinate matrix in opengl
 * @author suzuki ryoutoku
 * @date 2007-12-30
 * @version 1.0
 */
void Coordinate::settingOpenGL(void)
{
  // transfer
  ::glTranslated(this->getM_pivot()->x,
				 this->getM_pivot()->y,
				 this->getM_pivot()->z);
  // rotate
  ::glMultMatrixd((double*)(this->getM_matrix()->m) );

  // scale
  ::glScaled(this->getM_scale()->x,
			 this->getM_scale()->y,
			 this->getM_scale()->z);

  // shift local origin 
  ::glTranslated(-1.0*(this->getM_local_origin()->x),
				 -1.0*(this->getM_local_origin()->y),
				 -1.0*(this->getM_local_origin()->z));
  
}
